Skip to content

feat(logger): enhance log level handling #1476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 1, 2023
Merged

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Jun 1, 2023

Description of your changes

This PR introduces the possibility for customers to set the Logger level at runtime as well as retrieving the current level both in string (i.e. DEBUG) and numeric (i.e. 8) notations.

Before this PR, customers could only set the log level when creating a new Logger instance. There are however use cases (see #1265) in which customers might want to change the log level of an existing Logger instance at runtime. Likewise, customers were also not able to get the current log level as its value was treated as internal by the utility.

The PR introduces two new methods and a public property to the Logger class:

Get current log level

The first method added is getLevelName(), this method returns the current log level as string:

import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger();

logger.getLevelName(); // returns "INFO"

Get current log level number value

The second change is a new public class property called level, which returns the numeric value of the current log level:

import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger();

logger.level; // returns 12

Set a new log level

The third one is a new setLogLevel() method. This method accepts a string (both lower and uppercase) and sets the corresponding log level.

import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger();

logger.setLogLevel('DEBUG');

If an invalid value is passed, the method will throw an error.


In order to support these methods and streamline the log level handling the PR also changes the Logger class to exclusively use numeric values internally. This, together with the changes above, aligns the Logger utility to the behavior found in the Python version of Powertools for AWS.

Finally, the PR changes the access modifier of the shouldPrint method from private to protected, so that customers can optionally extend the method when subclassing the Logger instance and implement custom logging decision logic (#1307 (comment)).

The PR also covers a couple of housekeeping items:

  • Replace Math.random with randomInt from node:crypto in log sampling decision
  • Added @deprecated notice to createLogger helper that informs of the function's deprecation in the next major release

Related issues, RFCs

Issue number: #1265

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my change is effective and works
  • The PR title follows the conventional commit semantics

Breaking change checklist

Is it a breaking change?: NO

  • I have documented the migration process
  • I have added, implemented necessary warnings (if it can live side by side)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@boring-cyborg boring-cyborg bot added logger This item relates to the Logger Utility tests PRs that add or change tests labels Jun 1, 2023
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Jun 1, 2023
@dreamorosi dreamorosi self-assigned this Jun 1, 2023
@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label Jun 1, 2023
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Jun 1, 2023
@dreamorosi dreamorosi linked an issue Jun 1, 2023 that may be closed by this pull request
2 tasks
@dreamorosi dreamorosi marked this pull request as ready for review June 1, 2023 16:09
@dreamorosi dreamorosi merged commit 0021536 into main Jun 1, 2023
@dreamorosi dreamorosi deleted the dreamorosi/issue1265 branch June 1, 2023 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature PRs that introduce new features or minor changes logger This item relates to the Logger Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Allow changing log level at runtime
1 participant